import styles from './more.less';
import { List, WhiteSpace, Toast } from 'antd-mobile';
import { connect } from 'dva';
import router from 'umi/router';
import { Component } from 'react';

class More extends Component {
  onClick = menu => {
    let targetMenu = [ '关于精天','常见问题', '联系我们'];
    let targetIndex = targetMenu.findIndex(i => i === menu.text);
    console.log('​More -> targetIndex', targetIndex);
    if (targetIndex > -1) {
      this.props.dispatch({
        type: 'cms/queryAllCms',
        callback: () => {
          this.props.dispatch({
            type: 'cms/save',
            payload: {
              activeTab: 4
            }
          });
          this.props.dispatch({
            type: 'cms/changeCurrentPostObj',
            payload: targetIndex
          });
          router.push(menu.url);
        }
      });
    } else {
      if (menu.text === '生活商城') {
        if(this.props.loginToken){
          window.location.href = `${menu.url}?token=${this.props.loginToken}`;
        }else{
          Toast.info('生活商城登录身份获取中，请稍后再试')
        }
        // Toast.info('敬请期待~');

      } else {
        router.push(menu.url);
      }
    }
  };
  render() {
    return (
      <div>
        {this.props.more.listData.map((list, listIndex) => (
          <div key={listIndex}>
            <List>
              {list.map(menu => {
                if (
                  navigator.userAgent.indexOf('Html5Plus') > -1 &&
                  menu.text === '精天APP下载'
                ) {
                  return null;
                } else {
                  return (
                    <List.Item
                      thumb={menu.iconSrc}
                      arrow="horizontal"
                      key={menu.text}
                      onClick={this.onClick.bind(this, menu)}
                    >
                      {menu.text}
                    </List.Item>
                  );
                }
              })}
            </List>
            <WhiteSpace size="lg" />
          </div>
        ))}
        <div className={styles.copyright}>
          <p>
            <a>版本1.0.3</a>
          </p>
          <p>华瑞金源版权所有</p>
          <p>copyright C 2018 JinRuiHuaYuan. All Rights Reserved</p>
          <p>
            <a href="http://www.miitbeian.gov.cn">京ICP备18055685号</a>
          </p>
        </div>
      </div>
    );
  }
}

export default connect(state => {
  return {
    more: state.more,
    loginToken: state.main.loginToken,
  };
})(More);
